package com.shumm.service.impl;

import com.shumm.common.Result;
import com.shumm.mapper.ClassicFormulaMapper;
import com.shumm.po.ClassicFormula;
import com.shumm.service.ClassicFormulaService;
import com.shumm.vo.FormulaAndCompositionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


public class ClassicFormulaServiceImpl implements ClassicFormulaService {

    //    @Qualifier("cfMapper")
    @Autowired
    private ClassicFormulaMapper mapper;

//    public void setMapper(ClassicFormulaMapper mapper) {
//        this.mapper = mapper;
//    }

//    public ClassicFormulaServiceImpl(ClassicFormulaMapper mapper) {
//        this.mapper = mapper;
//    }

    @Override
    public Result add(ClassicFormula cf) {
        ClassicFormula classicFormula = mapper.selectByName(cf.getName());
        String message = "经方名称已存在";
        boolean existed = classicFormula.getName().equals(cf.getName());
        if (!existed) {
            int add = mapper.add(cf);
            message = "success";
        }
        return Result.success(message, null);
    }

    @Override
    public Result delete(int id) {
        String message = "删除失败";
        int delete = mapper.delete(id);
        if (delete > 0) {
            message = "删除成功";
        }
        return Result.success(message, null);
    }

    @Override
    public Result update(ClassicFormula cf) {
        String message = "更新失败";
        int update = mapper.update(cf);
        if (update > 0) {
            message = "更新成功";
        }
        return Result.success(message, null);
    }

    @Override
    public Result selectById(int id) {
        ClassicFormula classicFormula = mapper.selectById(id);
        return Result.success(classicFormula);
    }

    @Override
    public Result selectAll() {
        List<ClassicFormula> list = mapper.selectAll();
        int t = 1 / 0;
        return Result.success(list);
    }

    @Override
    public Result selectByName(String name) {
        ClassicFormula classicFormula = mapper.selectByName(name);
        return Result.success(classicFormula);
    }

    @Override
    public Result selectByAlias(String alias) {
        List<ClassicFormula> classicFormulas = mapper.selectByAlias(alias);
        return Result.success(classicFormulas);
    }

    @Override
    public Result selectByChineseMedicine(String cm) {
        List<ClassicFormula> classicFormulas = mapper.selectByChineseMedicine(cm);
        return Result.success(classicFormulas);
    }

    public Result selectComById(int id) {
        List<FormulaAndCompositionVO> formulaAndCompositionVOS = mapper.selectComById(id);
        return Result.success(formulaAndCompositionVOS);
    }
}
